﻿using System;
using System.IO;
using System.Threading.Tasks;
using Minio;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Minio.DataModel.Args;

namespace WMS_MAV.Controllers
{
    public class MinIOController : Controller
    {
        private readonly IConfiguration _configuration;
        private readonly ILogger<MinIOController> _logger;
        private MinioClient _minioClient;
        public MinIOController(IConfiguration configuration, ILogger<MinIOController> logger)
        {
            _configuration = configuration;
            _logger = logger;
            var endpoint = _configuration["Minio:Endpoint"];
            var accessKey = _configuration["Minio:AccessKey"];
            var secretKey = _configuration["Minio:SecretKey"];
  //          var bucketName = _configuration["Minio:BucketName"];
  //          _minioClient = new MinioClient()
  //.WithEndpoint(endpoint)
  //.WithCredentials(accessKey, secretKey)
  //.Build()
        }


        public IActionResult Index()
        {
            return View();
        }
        public IActionResult MinioTest()
        {
            return View();
        }
        /// <summary>
        /// minio断点续传
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        //public async Task<IActionResult> Upload()
        //{
        //    try
        //    {
        //        var file = Request.Form.Files[0];
        //        if (file == null || file.Length == 0)
        //        {
        //            return BadRequest("请选择要上传的文件");
        //        }

        //        // 检查桶是否存在，不存在则创建
        //        var beArgs = new BucketExistsArgs().WithBucket(bucketName);
        //        bool found = await _minioClient.BucketExistsAsync(beArgs);
        //        if (!found)
        //        {
        //            var mbArgs = new MakeBucketArgs().WithBucket(bucketName);
        //            await _minioClient.MakeBucketAsync(mbArgs);
        //        }

        //        // 获取文件已上传的部分大小（可从请求头获取）
        //        var offset = Request.Headers.ContainsKey("Range") ? long.Parse(Request.Headers["Range"].ToString().Split("=")[1]) : 0;

        //        var objectName = Path.GetFileName(file.FileName);
        //        var putObjectArgs = new PutObjectArgs()
        //           .WithBucket(bucketName)
        //           .WithObject(objectName)
        //           .WithStream(file.OpenReadStream(), file.Length, offset)
        //           .WithContentType(file.ContentType);
        //        await _minioClient.PutObjectAsync(putObjectArgs);

        //        return Ok("文件上传成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError(ex, "文件上传出错");
        //        return StatusCode(500, "文件上传失败");
        //    }
        //}
    }
}
